<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Validations</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Validations</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activerecord/lib/active_record/validations_rb.html">
                vendor/rails/activerecord/lib/active_record/validations.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Active Records implement validation by overwriting Base#validate (or the
variations, <tt><a
href="Validations.html#M001317">validate_on_create</a></tt> and <tt><a
href="Validations.html#M001318">validate_on_update</a></tt>). Each of these
methods can inspect the state of the object, which usually means ensuring
that a number of attributes have a certain value (such as not empty, within
a given range, matching a certain regular expression).
</p>
<p>
Example:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    protected
      def validate
        errors.add_on_empty %w( first_name last_name )
        errors.add(&quot;phone_number&quot;, &quot;has invalid format&quot;) unless phone_number =~ /[0-9]*/
      end

      def validate_on_create # is only run the first time a new object is saved
        unless valid_discount?(membership_discount)
          errors.add(&quot;membership_discount&quot;, &quot;has expired&quot;)
        end
      end

      def validate_on_update
        errors.add_to_base(&quot;No changes have occurred&quot;) if unchanged_attributes?
      end
  end

  person = Person.new(&quot;first_name&quot; =&gt; &quot;David&quot;, &quot;phone_number&quot; =&gt; &quot;what?&quot;)
  person.save                         # =&gt; false (and doesn't do the save)
  person.errors.empty?                # =&gt; false
  person.errors.count                 # =&gt; 2
  person.errors.on &quot;last_name&quot;        # =&gt; &quot;can't be empty&quot;
  person.errors.on &quot;phone_number&quot;     # =&gt; &quot;has invalid format&quot;
  person.errors.each_full { |msg| puts msg }
                                      # =&gt; &quot;Last name can't be empty\n&quot; +
                                           &quot;Phone number has invalid format&quot;

  person.attributes = { &quot;last_name&quot; =&gt; &quot;Heinemeier&quot;, &quot;phone_number&quot; =&gt; &quot;555-555&quot; }
  person.save # =&gt; true (and person is now saved in the database)
</pre>
<p>
An <tt><a href="Errors.html">Errors</a></tt> object is automatically
created for every Active Record.
</p>
<p>
Please do have a look at <a
href="Validations/ClassMethods.html">ActiveRecord::Validations::ClassMethods</a>
for a higher level of validations.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001315">errors</a>&nbsp;&nbsp;
      <a href="#M001311">save_with_validation</a>&nbsp;&nbsp;
      <a href="#M001312">save_with_validation!</a>&nbsp;&nbsp;
      <a href="#M001313">update_attribute_with_validation_skipping</a>&nbsp;&nbsp;
      <a href="#M001314">valid?</a>&nbsp;&nbsp;
      <a href="#M001316">validate</a>&nbsp;&nbsp;
      <a href="#M001317">validate_on_create</a>&nbsp;&nbsp;
      <a href="#M001318">validate_on_update</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="Validations/ClassMethods.html" class="link">ActiveRecord::Validations::ClassMethods</a><br />

    </div>

    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">VALIDATIONS</td>
          <td>=</td>
          <td class="context-item-value">%w( validate validate_on_create validate_on_update )</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001315" class="method-detail">
        <a name="M001315"></a>

        <div class="method-heading">
          <a href="#M001315" class="method-signature">
          <span class="method-name">errors</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the <a href="Errors.html">Errors</a> object that holds all
information about attribute error messages.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001315-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001315-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 977</span>
977:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">errors</span>
978:       <span class="ruby-ivar">@errors</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Errors</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
979:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001311" class="method-detail">
        <a name="M001311"></a>

        <div class="method-heading">
          <a href="#M001311" class="method-signature">
          <span class="method-name">save_with_validation</span><span class="method-args">(perform_validation = true)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
The validation process on save can be skipped by passing false. The regular
<a href="Base.html#M001430">Base#save</a> method is replaced with this when
the validations module is mixed in, which it is by default.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001311-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001311-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 932</span>
932:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save_with_validation</span>(<span class="ruby-identifier">perform_validation</span> = <span class="ruby-keyword kw">true</span>)
933:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">perform_validation</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">valid?</span> <span class="ruby-operator">||</span> <span class="ruby-operator">!</span><span class="ruby-identifier">perform_validation</span>
934:         <span class="ruby-identifier">save_without_validation</span>
935:       <span class="ruby-keyword kw">else</span>
936:         <span class="ruby-keyword kw">false</span>
937:       <span class="ruby-keyword kw">end</span>
938:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001312" class="method-detail">
        <a name="M001312"></a>

        <div class="method-heading">
          <a href="#M001312" class="method-signature">
          <span class="method-name">save_with_validation!</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Attempts to save the record just like <a
href="Base.html#M001430">Base#save</a> but will raise a RecordInvalid
exception instead of returning false if the record is not valid.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001312-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001312-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 942</span>
942:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save_with_validation!</span>
943:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">valid?</span>
944:         <span class="ruby-identifier">save_without_validation!</span>
945:       <span class="ruby-keyword kw">else</span>
946:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">RecordInvalid</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
947:       <span class="ruby-keyword kw">end</span>
948:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001313" class="method-detail">
        <a name="M001313"></a>

        <div class="method-heading">
          <a href="#M001313" class="method-signature">
          <span class="method-name">update_attribute_with_validation_skipping</span><span class="method-args">(name, value)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Updates a single attribute and saves the record without going through the
normal validation procedure. This is especially useful for boolean flags on
existing records. The regular <tt>update_attribute</tt> method in <a
href="Base.html">Base</a> is replaced with this when the validations module
is mixed in, which it is by default.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001313-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001313-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 953</span>
953:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_attribute_with_validation_skipping</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">value</span>)
954:       <span class="ruby-identifier">send</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'='</span>, <span class="ruby-identifier">value</span>)
955:       <span class="ruby-identifier">save</span>(<span class="ruby-keyword kw">false</span>)
956:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001314" class="method-detail">
        <a name="M001314"></a>

        <div class="method-heading">
          <a href="#M001314" class="method-signature">
          <span class="method-name">valid?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Runs <a href="Validations.html#M001316">validate</a> and <a
href="Validations.html#M001317">validate_on_create</a> or <a
href="Validations.html#M001318">validate_on_update</a> and returns true if
no <a href="Validations.html#M001315">errors</a> were added otherwise
false.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001314-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001314-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 959</span>
959:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">valid?</span>
960:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">clear</span>
961: 
962:       <span class="ruby-identifier">run_validations</span>(<span class="ruby-identifier">:validate</span>)
963:       <span class="ruby-identifier">validate</span>
964: 
965:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">new_record?</span>
966:         <span class="ruby-identifier">run_validations</span>(<span class="ruby-identifier">:validate_on_create</span>)
967:         <span class="ruby-identifier">validate_on_create</span>
968:       <span class="ruby-keyword kw">else</span>
969:         <span class="ruby-identifier">run_validations</span>(<span class="ruby-identifier">:validate_on_update</span>)
970:         <span class="ruby-identifier">validate_on_update</span>
971:       <span class="ruby-keyword kw">end</span>
972: 
973:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span>
974:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Protected Instance methods</h3>

      <div id="method-M001316" class="method-detail">
        <a name="M001316"></a>

        <div class="method-heading">
          <a href="#M001316" class="method-signature">
          <span class="method-name">validate</span><span class="method-args">(</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Overwrite this method for validation checks on all saves and use <a
href="Errors.html#M001346">Errors.add</a>(field, msg) for invalid
attributes.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001316-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001316-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 983</span>
983:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validate</span> <span class="ruby-comment cmt">#:doc:</span>
984:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001317" class="method-detail">
        <a name="M001317"></a>

        <div class="method-heading">
          <a href="#M001317" class="method-signature">
          <span class="method-name">validate_on_create</span><span class="method-args">(</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Overwrite this method for validation checks used only on creation.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001317-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001317-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 987</span>
987:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validate_on_create</span> <span class="ruby-comment cmt">#:doc:</span>
988:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001318" class="method-detail">
        <a name="M001318"></a>

        <div class="method-heading">
          <a href="#M001318" class="method-signature">
          <span class="method-name">validate_on_update</span><span class="method-args">(</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Overwrite this method for validation checks used only on updates.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001318-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001318-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 991</span>
991:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validate_on_update</span> <span class="ruby-comment cmt"># :doc:</span>
992:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>